package com.logos.cartoon.ui;

/**
 * @author logos
 *         date 2025/5/24 13:09
 * @version v1.0
 * @Package com.logos.cartoon.ui
 */
import com.formdev.flatlaf.FlatDarkLaf;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.io.IOException;
import java.net.URL;

@Component
public class HomeFrame extends JFrame {
    public HomeFrame() throws IOException {
        initUI();
    }

    private void initUI() throws IOException {
        try {
            UIManager.setLookAndFeel(new FlatDarkLaf()); // 更换为暗色主题
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        setTitle("首页 - 二次元角色图鉴");
        setSize(1000, 800);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // 在设置外观之后立即创建窗口形状
        SwingUtilities.invokeLater(() -> {
            setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), 20, 20));
        });

        // 主面板
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 顶部面板
        JPanel headerPanel = createHeaderPanel();
        mainPanel.add(headerPanel, BorderLayout.NORTH);

        // 中间面板
        JPanel centerPanel = createCenterPanel();
        mainPanel.add(centerPanel, BorderLayout.CENTER);

        setContentPane(mainPanel);
        SwingUtilities.updateComponentTreeUI(this);
        setVisible(true);
    }

    private JPanel createHeaderPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));

        // 修改标题字体和颜色
        JLabel titleLabel = new JLabel("动漫世界");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 36)); // 更大更粗的字体
        titleLabel.setForeground(Color.DARK_GRAY); // 更改颜色

        JTextField searchField = new JTextField(20);
        searchField.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));

        JButton searchButton = new JButton("搜索角色");
        searchButton.setBackground(Color.BLUE);
        searchButton.setForeground(Color.WHITE);
        searchButton.setFocusPainted(false);

        panel.add(titleLabel);
        panel.add(Box.createHorizontalStrut(20));
        panel.add(searchField);
        panel.add(Box.createHorizontalStrut(10));
        panel.add(searchButton);

        // 在 createHeaderPanel 方法中添加关闭按钮
        JButton closeButton = new JButton("×");
        closeButton.setFont(new Font("Arial", Font.BOLD, 20));
        closeButton.setForeground(Color.RED);
        closeButton.setContentAreaFilled(false);
        closeButton.setBorderPainted(false);
        closeButton.addActionListener(e -> System.exit(0));
        panel.add(Box.createHorizontalGlue());
        panel.add(closeButton);
        return panel;
    }

    private JPanel createCenterPanel() throws IOException {
        JPanel panel = new JPanel(new GridLayout(2, 2, 20, 20));

        // 自定义边框样式
        Border customBorder = BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(Color.GRAY, 2), // 外层灰色边框
                BorderFactory.createEmptyBorder(5, 5, 5, 5) // 内部填充
        );

        // 热度 TOP3 角色
        JPanel top3Panel = createTop3Panel();
        top3Panel.setBorder(BorderFactory.createTitledBorder(customBorder, "热度 TOP3 角色"));

        // 今日幸运之星
        JPanel luckyPanel = createLuckyPanel();
        luckyPanel.setBorder(BorderFactory.createTitledBorder(customBorder, "今日幸运之星"));

        // 热门角色
        JPanel hotPanel = createHotPanel();
        hotPanel.setBorder(BorderFactory.createTitledBorder(customBorder, "热门角色"));

        panel.add(top3Panel);
        panel.add(luckyPanel);
        panel.add(hotPanel);

        return panel;
    }

    private JPanel createTop3Panel() throws IOException {
        JPanel panel = new JPanel(new GridLayout(1, 3, 20, 20));
        String[] names = { "艾伦", "佐助", "阿斯兰" };
        String[] descriptions = { "7赞", "2赞", "1赞" };

        ImageIcon[] scaledImages = loadAndScaleImages();

        for (int i = 0; i < 3; i++) {
            panel.add(createCharacterPanel(names[i], descriptions[i], scaledImages[i], i + 1));
        }

        return panel;
    }

    private JPanel createLuckyPanel() throws IOException {
        JPanel panel = new JPanel(new BorderLayout());
        JLabel starLabel = new JLabel("★");
        starLabel.setFont(new Font("Arial", Font.BOLD, 24));
        starLabel.setForeground(Color.ORANGE);

        ImageIcon[] scaledImages = loadAndScaleImages();
        JLabel imageLabel = new JLabel(scaledImages[0]);

        JLabel nameLabel = new JLabel("艾伦");
        JLabel descLabel = new JLabel("海的那边是敌人");

        JPanel infoPanel = new JPanel(new BorderLayout());
        infoPanel.add(nameLabel, BorderLayout.NORTH);
        infoPanel.add(descLabel, BorderLayout.SOUTH);

        JPanel contentPanel = new JPanel(new BorderLayout());
        contentPanel.add(starLabel, BorderLayout.WEST);
        contentPanel.add(imageLabel, BorderLayout.CENTER);
        contentPanel.add(infoPanel, BorderLayout.EAST);

        panel.add(contentPanel, BorderLayout.CENTER);
        return panel;
    }

    private JPanel createHotPanel() throws IOException {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        String[] names = { "艾伦", "佐助", "阿斯兰" };
        String[] descriptions = { "7赞", "2赞", "1赞" };
        ImageIcon[] scaledImages = loadAndScaleImages();

        for (int i = 0; i < 3; i++) {
            panel.add(createCharacterPanel(names[i], descriptions[i], scaledImages[i], 0));
            panel.add(Box.createRigidArea(new Dimension(0, 10)));
        }

        return panel;
    }

    private JPanel createCharacterPanel(String name, String desc, ImageIcon image, int number) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));

        JLabel imageLabel = new JLabel(image);
        panel.add(imageLabel, BorderLayout.CENTER);

        if (number > 0) {
            JLabel numberLabel = new JLabel(String.valueOf(number));
            numberLabel.setFont(new Font("Arial", Font.BOLD, 18));
            numberLabel.setForeground(Color.ORANGE);
            panel.add(numberLabel, BorderLayout.NORTH);
        }

        JPanel infoPanel = new JPanel(new BorderLayout());
        JLabel nameLabel = new JLabel(name);
        JLabel descLabel = new JLabel(desc);
        infoPanel.add(nameLabel, BorderLayout.NORTH);
        infoPanel.add(descLabel, BorderLayout.SOUTH);
        panel.add(infoPanel, BorderLayout.SOUTH);

        return panel;
    }

    private ImageIcon[] loadAndScaleImages() throws IOException {
        ImageIcon[] images = {
                new ImageIcon(new URL(
                        "https://web-rose25.oss-cn-beijing.aliyuncs.com/08145663-36a9-489d-831b-c8e69ebf8e41.jpg")),
                new ImageIcon("path/to/sasuke.jpg"),
                new ImageIcon("path/to/aslan.jpg")
        };

        ImageIcon[] scaledImages = new ImageIcon[3];
        for (int i = 0; i < 3; i++) {
            Image scaledImage = images[i].getImage().getScaledInstance(100, 100, Image.SCALE_SMOOTH);
            scaledImages[i] = new ImageIcon(scaledImage);
        }

        return scaledImages;
    }
}
